package com.czk.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * jdk1.8时间格式化的线程安全类
 *
 * @Author:ChenZhangKun
 * @Date: 2021/9/25 15:36
 */
public class DateUtilJava8 {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter localDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static LocalDateTime parse(String target) {
        return LocalDateTime.parse(target, formatter);
    }

    public static LocalDate parse_1(String target) {
        return LocalDate.parse(target, localDateFormatter);
    }

    public static String format(LocalDateTime localDateTime) {
        return localDateTime.format(formatter);
    }

    public static String format(LocalDate localDate) {
        return localDate.format(localDateFormatter);
    }

    public static void main(String[] args) {
        // 转为日期时间
        System.out.println(DateUtilJava8.parse("2020-01-11 12:12:08"));
        // 转为当前日期
        System.out.println(DateUtilJava8.parse_1("2020-01-11"));
        // 转为日期字符串
        System.out.println(DateUtilJava8.format(DateUtilJava8.parse("2020-01-11 12:12:08")));
        // 转为
        System.out.println(DateUtilJava8.format(DateUtilJava8.parse_1("2020-01-11")));
    }
}
